package com.simplemobiletools.camera.extensions

import android.app.Activity
import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.simplemobiletools.camera.helpers.AppConfig
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.isOnMainThread
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

val Context.appConfig: AppConfig get() = AppConfig.newInstance(applicationContext)

val Context.frontCameraId: Int get() = CameraCharacteristics.LENS_FACING_FRONT

val Context.backCameraId: Int get() = CameraCharacteristics.LENS_FACING_BACK

fun Context.countOfCameras(): Int? {
    return try {
        val manager = this.getSystemService(Context.CAMERA_SERVICE) as CameraManager
        manager.cameraIdList.size
    } catch (e: Exception) {
        null
    }
}

fun Context.getOutputMediaFile(isPhoto: Boolean): String {
    val mediaStorageDir = File(appConfig.savePhotosFolder)

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return ""
        }
    }

    val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
    return if (isPhoto) {
        "${mediaStorageDir.path}/IMG_$timestamp.jpg"
    } else {
        "${mediaStorageDir.path}/VID_$timestamp.mp4"
    }
}
